package bokk;

import java.util.Scanner;

public class Library {
    private Book[] books ;// 存储图书的数组
    private int capacity;
    private int bookCount; // 当前图书数量

    public Library() {
        this.capacity = 10;
        books = new Book[capacity]; // 初始化图书数组
        books[0] = new Book("三国演义", "张三", 2020, 45.0);
        books[1] = new Book("水浒传", "李四", 2019, 38.0);
        books[2] = new Book("红楼梦", "王五", 2022, 50.0);
        books[3] = new Book("西游记","吴承恩",1998,88);
        books[4] = new Book("SQL从入门到入坟","老刘",2818,66);
        bookCount =5 ; // 初始化图书数量为0
    }

    public Library(Book[] books){
        this.books = books;
    }

    // 添加图书到图书馆
    public boolean addBook(Book book) {
        if (bookCount < books.length - 1) {
            books[bookCount] = book;
            bookCount++;
            return true;
        } else {
            System.out.println("图书馆已满，无法添加新图书。");
            return false;
        }
    }

    // 根据书名查找图书
    public Book findBook(String title) {
        if (title == null) {
            return null; // 如果找不到则返回null
        }
        for (int i = 0; i < bookCount; i++) {
            if (title.equals(books[i].getTitle())) {
                return books[i];
            }
        }
        return null;
    }

    // 显示图书馆中所有图书
    public void displayBooks() {
        if (bookCount == 0) {
            System.out.println("图书馆中没有图书。");
            return;
        }
        System.out.println("图书馆中的图书列表：");
        for (int i = 0; i < bookCount; i++) {
            Book book = books[i];
            System.out.println("书名：" + book.getTitle() +
                    " 作者：" + book.getAuthor() +
                    " 出版年份：" + book.getYear() +
                    " 价格：" + book.getPrice());
        }
    }
}

class LibraryTest {
    private static Library lib = new Library();
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            menu();
            System.out.println("请输入你的操作类型：");
            int op = sc.nextInt();
            switch (op) {
                case 1 -> showAll();
                case 2 -> addBook();
                case 3 -> selectBook();
                case 4 -> {
                    System.out.println("退出成功");
                    return;
                }
            }
        }
    }
    public static void menu() {
        System.out.println("*******************");
        System.out.println("1. 查看所有图书");
        System.out.println("2. 添加图书");
        System.out.println("3. 查询图书");
        System.out.println("4. 退出");
        System.out.println("*******************");
    }
    public static void showAll() {
        lib.displayBooks();
    }
    public static void addBook(){
        System.out.println("请输入添加的书籍：");
        String title = sc.next();
        System.out.println("请输入作者：");
        String auhtor = sc.next();
        System.out.println("请输入年限：");
        int year = sc.nextInt();
        System.out.println("请输入价格：");
        double price = sc.nextDouble();
        Book book= new Book(title,auhtor,year,price);
        if (lib.addBook(book)) {
            System.out.println("添加成功！" + book.getTitle());
        } else {
            System.out.println("添加失败");
        }
    }
    public static void selectBook(){
        System.out.println("请输入你要查找的书籍：");
        String title = sc.next();
        Book book = lib.findBook(title);
        Book foundBook = lib.findBook(title);
        if (foundBook != null) {
            System.out.println("找到了书名为《" + title + "》的图书。"  +"\n" +
                    " 作者：" + book.getAuthor() +
                    " 出版年份：" + book.getYear() +
                    " 价格：" + book.getPrice());
        } else {
            System.out.println("没有找到书名为《" + title + "》的图书。");
        }

    }

}
